//
//  ChangePassWordViewController.m
//  XDCommonApp
//
//  Created by wanglong8889@126.com on 14-3-12.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "ChangePassWordViewController.h"
#import "XDTools.h"
#import "XDHeader.h"
#define XZB  75
@interface ChangePassWordViewController ()
{
    NSUserDefaults *Defaults;
}
@end

@implementation ChangePassWordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initViews];
	// Do any additional setup after loading the view.
}
-(void)initViews{

        CGRect rect = self.contentView.frame;
        rect.size.height+=44;
        self.contentView.frame = rect;
 
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width
                                                                     , self.contentView.frame.size.height)];
    _scrollView.scrollEnabled = NO;
    [self.contentView addSubview:_scrollView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelPan)];
    [self changeFrameWhenHiddenNavigationBar];
    [self.contentView addGestureRecognizer:tap];
    
    Defaults = [NSUserDefaults standardUserDefaults];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(XZB, 90,110, 25)];
    titleLabel.text = @"修改密码";
    titleLabel.font = [UIFont fontWithName:@"Helvetica" size:22.0];
    titleLabel.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:titleLabel];
    
    UILabel *userLabel = [[UILabel alloc] initWithFrame:CGRectMake(XZB, titleLabel.frame.origin.y+titleLabel.frame.size.height+5, 145, 17)];
    userLabel.backgroundColor = [UIColor clearColor];
    userLabel.text = [NSString stringWithFormat:@"当前用户:%@",[Defaults objectForKey:@"nickName"]];
    userLabel.font = [UIFont systemFontOfSize:14];
    userLabel.textColor = TEXTBLACKCOLORE4;
    [self.scrollView addSubview:userLabel];
    
    UILabel *pwdLabel = [[UILabel alloc] initWithFrame:CGRectMake(XZB, titleLabel.frame.origin.y+titleLabel.frame.size.height+45,120, 15)];
    pwdLabel.text = @"请输入旧密码";
    pwdLabel.font = [UIFont systemFontOfSize:13.0];
    pwdLabel.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:pwdLabel];
    
    oldTf = [[UITextField alloc] initWithFrame:CGRectMake(XZB, pwdLabel.frame.origin.y+pwdLabel.frame.size.height+10,187, 28)];
    oldTf.borderStyle = UITextBorderStyleLine;
    oldTf.backgroundColor = RGBA(168, 168, 168, 1.0);
    oldTf.textColor = [UIColor whiteColor];
    if (IOS7) {
        oldTf.tintColor = [UIColor whiteColor];
    }
    oldTf.font = [UIFont systemFontOfSize:13.0];
    oldTf.delegate = self;
    oldTf.secureTextEntry = YES;
//    oldTf.placeholder = @"请输入旧密码";
    oldTf.returnKeyType = UIReturnKeyDone;
    oldTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.scrollView addSubview:oldTf];
    
    UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(XZB, oldTf.frame.origin.y+oldTf.frame.size.height+20,90, 15)];
    newLabel.text = @"请输入新密码";
    newLabel.font = [UIFont systemFontOfSize:13.0];
    newLabel.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:newLabel];
    
    newTf = [[UITextField alloc] initWithFrame:CGRectMake(XZB, newLabel.frame.origin.y+newLabel.frame.size.height+10,187, 28)];
    newTf.borderStyle = UITextBorderStyleLine;
    newTf.backgroundColor =  RGBA(168, 168, 168, 1.0);
    newTf.secureTextEntry = YES;
    newTf.textColor = [UIColor whiteColor];
    if (IOS7) {
        newTf.tintColor = [UIColor whiteColor];
    }
    newTf.delegate = self;
    newTf.font = [UIFont systemFontOfSize:13.0];
    newTf.returnKeyType = UIReturnKeyDone;
//     newTf.placeholder = @"请输入新密码";
    newTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.scrollView addSubview:newTf];
    
    UILabel *confirmLabel = [[UILabel alloc] initWithFrame:CGRectMake(XZB, newTf.frame.origin.y+newTf.frame.size.height+20,100, 15)];
    confirmLabel.text = @"再次输入新密码";
    confirmLabel.font = [UIFont systemFontOfSize:13.0];
    confirmLabel.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:confirmLabel];
    
    confirmTf = [[UITextField alloc] initWithFrame:CGRectMake(XZB, confirmLabel.frame.origin.y+confirmLabel.frame.size.height+10,187, 28)];
    confirmTf.borderStyle = UITextBorderStyleLine;
    confirmTf.backgroundColor =  RGBA(168, 168, 168, 1.0);
    confirmTf.textColor = [UIColor whiteColor];
    if (IOS7) {
        confirmTf.tintColor = [UIColor whiteColor];
    }
    confirmTf.secureTextEntry = YES;
    confirmTf.delegate = self;
    confirmTf.font = [UIFont systemFontOfSize:13.0];
    confirmTf.returnKeyType = UIReturnKeyDone;
//    confirmTf.placeholder = @"再次输入新密码";
    confirmTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.scrollView addSubview:confirmTf];
    
    
    UILabel *continueLabel = [[UILabel alloc] initWithFrame:CGRectMake(XZB+135, confirmTf.frame.origin.y+confirmTf.frame.size.height+31, 30,15)];
    continueLabel.text = @"完成";
    continueLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
    continueLabel.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:continueLabel];
    
    UIImageView *arImageView = [[UIImageView alloc] initWithFrame:CGRectMake(continueLabel.frame.origin.x+continueLabel.frame.size.width+6, continueLabel.frame.origin.y-2, 12, 18)];
    arImageView.image = [UIImage imageNamed:@"launh_sure"];
    [self.scrollView addSubview:arImageView];
    
    UIButton *continueBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    continueBtn.frame = CGRectMake(XZB+130, confirmTf.frame.origin.y+confirmTf.frame.size.height+26, 50,28);
    [continueBtn addTarget:self action:@selector(continueNex) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:continueBtn];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(8, self.contentView.frame.size.height-45, 45, 45);
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *bImageView = [[UIImageView alloc] initWithFrame:CGRectMake(24, self.contentView.frame.size.height-32, 11, 18)];
    bImageView.image = [UIImage imageNamed:@"left_back"];
    [self.scrollView addSubview:bImageView];
    [self.scrollView addSubview:backBtn];
}
//继续
-(void)continueNex{
    
//    uID = @"1000";
//    DDLOG(@"uID = %@ workID = %@  com  = %@",uID,workID,_comStr);
//    NSString *UID_STR = [NSString stringWithFormat:@"%@",[[Defaults objectForKey:kMY_USERINFO]objectForKey:kMY_USER_ID]];
    NSString *UID_STR = [NSString stringWithFormat:@"%@",[Defaults objectForKey:kMY_USER_ID]];

    DDLOG(@"uid - = %@  pass =%@",UID_STR,[[Defaults valueForKey:kMY_USERINFO]valueForKey:kMY_USER_PASSWORD]);
//    Defaults removeObjectForKey:[]
    if (oldTf.text.length == 0||newTf.text.length == 0||confirmTf.text.length == 0) {
        [XDTools showTips:@"信息不完整" toView:self.contentView];
        return;
    }
    if (![newTf.text isEqualToString:confirmTf.text]) {
        [XDTools showTips:@"两次密码输入不一致" toView:self.contentView];
        return;
    }
  
    if ([XDTools NetworkReachable]){
        NSString *body = [NSString stringWithFormat:@" <ChangePassword xmlns=\"http://tempuri.org/\">"
                          "<userId>%@</userId>"
                          "<oldPassword>%@</oldPassword>"
                          "<newPassword>%@</newPassword>"
                          "</ChangePassword>",UID_STR,oldTf.text,confirmTf.text];
        
        __weak ASIFormDataRequest *request = [XDTools postRequestWithDict:body API:CHANGEPASSWORD];
        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            //[XDTools hideProgress:self.contentView];
            NSString *responseString = [request responseString];
            // NSDictionary *tempDic = [XDTools  JSonFromString:responseString];
            DDLOG(@"responseString:%@", responseString);
            
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            int statusCode = [request responseStatusCode];
            NSString *soapAction = [[request requestHeaders] objectForKey:@"SOAPAction"];
            
            NSArray *arraySOAP =[soapAction componentsSeparatedByString:@"/"];
            int count = [arraySOAP count] - 1;
            NSString *methodName = [arraySOAP objectAtIndex:count];
            
            // Use when fetching text data
            NSString *result = nil;
            if (statusCode == 200) {
                //表示正常请求
                result = [SoapXmlParseHelper SoapMessageResultXml:responseString ServiceMethodName:methodName];
                NSDictionary  *responseDict = [XDTools JSonFromString:result];
                if ([[responseDict objectForKey:@"code"] intValue]==0){
                    DDLOG(@"res = %@",responseDict);
                    
                    [XDTools showTips:@"修改成功" toView:self.contentView];

                     [self performSelector:@selector(back) withObject:nil afterDelay:.5];
//                    [self.navigationController popViewControllerAnimated:YES];

                }else{
                    [XDTools showTips:@"修改失败" toView:self.contentView];
                }
            }
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
        }];
        
        [request startAsynchronous];
        [XDTools showProgress:self.contentView];
    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }

}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)cancelPan{
    [oldTf resignFirstResponder];
    [newTf resignFirstResponder];
    [confirmTf resignFirstResponder];
    [UIView animateWithDuration:.3 animations:^{
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }];
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self cancelPan];
    return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [UIView animateWithDuration:.3 animations:^{
        self.scrollView.contentOffset = CGPointMake(0, 120);
    }];
    
    return  YES;
}
//完成

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
